package com.siecomzg.transport.handler;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import com.siecomzg.constconfig.ConstDefine;
import com.siecomzg.proto.Message;
import com.siecomzg.tool.LogUtil;
import com.siecomzg.transport.CallbackMap;
import com.siecomzg.transport.CallbackTask;
import com.siecomzg.transport.CommonClient;
import com.siecomzg.transport.DeviceCallback;
import com.siecomzg.transport.SiecomDevice;

import java.nio.ByteBuffer;
import java.util.HashMap;

/**
 * Created on 2017/1/18.
 */

public class FingerPrintHandler extends CommonHandler {
    public static final  String TAG = "FingerPrintHandler";

    @Override
    public void onReceive(CommonClient server, Message.siecomproto msg) {
        String msgid = msg.getMsgid();
        HashMap<String, CallbackTask> callbackTaskHashMap = CallbackMap.getCallbackTaskHashMap();
        if(callbackTaskHashMap.containsKey(msgid)){
            CallbackTask callbackTask = callbackTaskHashMap.get(msgid);
            callbackTask.getTimer().cancel();
            DeviceCallback callback = callbackTask.getCallback();
            boolean isOnMain = callbackTask.isOnMain();
            int respondCmd = msg.getCmd();

            if(respondCmd== ConstDefine.FINGERFEARURE_RESPOND){
                readFingerFeature(msg,callback,isOnMain);
            }
            //注册指纹的返回
            if (respondCmd == ConstDefine.FINGER_REGIST_RESPOND){
                fingerRegist(msg,callback,isOnMain);
            }

            if(respondCmd== ConstDefine.FINGER_PIC_RESPOND){
                readFingerPic(msg,callback,isOnMain);
            }
            if(respondCmd==ConstDefine.FINGER_RESPOND){
                readFingerVer(msg,callback,isOnMain);
            }
            if(msg.getCmd()==ConstDefine.FINGER_CANCEL_RESPOND){
                cancelCall(msg,callback, isOnMain);
            }
            if(msg.getCmd()==ConstDefine.TEMPLET_RESPOND){
                getTemplet(msg,callback, isOnMain);
            }
            if(msg.getCmd()==ConstDefine.MATCH_RESPOND){
                match(msg,callback, isOnMain);
            }
            callbackTaskHashMap.remove(msgid);
        }

    }
    private void match(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain){
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message",  msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }
    private void getTemplet(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain){
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message",  msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }
    private  void  readFingerPic(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain){
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            byte[] imgData = msg.getExtion().toByteArray() ;
            int type = Integer.parseInt(msg.getRequest());
            Bitmap bitmap = null;
            LogUtil.e("bmpType",type+"==");
            if(type==1) {
//                bitmap = Bytes2Bimap(imgData);
                bitmap = tesoByte2bitmap(imgData);
            } else
            if(type==2||type==3){
                bitmap =  BitmapFactory.decodeByteArray(imgData, 0, imgData.length);
            }
            else {
                bitmap = Bytes2Bimap(imgData);
            }
            bundle.putParcelable("Img",bitmap);
            bundle.putParcelable("bitmap",bitmap);
            bundle.putString("data", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            LogUtil.e("errno",errno+"==="+msg.getRespond());
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message",  msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);


    }
    public static Bitmap Bytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }

    private  static Bitmap tesoByte2bitmap(byte[] byBuffer){

        if(byBuffer.length>4000){
            Bitmap bitmap = BitmapFactory.decodeByteArray(
                    byBuffer, 0, byBuffer.length);
            return bitmap;
        }
        int bmpWidth = 152;
        int bmpHeigh = 200;
        Bitmap bmpFingerprint = Bitmap.createBitmap(bmpWidth, bmpHeigh,
                Bitmap.Config.ARGB_8888);
        int iPos = 0;
        byte[] buf = new byte[bmpWidth * bmpHeigh * 4];
        for (int x = 0; x < bmpWidth; x++) {
            for (int y = 0; y < bmpHeigh; y++) {
                iPos = y * bmpWidth + x;

                byte[] t = new byte[4];
                int pos = iPos / 8;

                byte c = byBuffer[pos];
                if ((byte) ((c >> (8 - iPos % 8)) & 0x1) == 0x00) {
                    //grep = 0xff;
                    t[0] = (byte) 0xff;
                    t[1] = (byte) 0xff;
                    t[2] = (byte) 0xff;
                    t[3] = (byte) 0xff;
                } else {
                    //grep = 0x00;
                    t[0] = (byte) 0x00;
                    t[1] = (byte) 0x00;
                    t[2] = (byte) 0x00;
                    t[3] = (byte) 0xff;
                }

                System.arraycopy(t, 0, buf, iPos * 4, t.length);
                //bmpFingerprint.setPixel(x, bmpHeigh - y - 1, iColor);
            }
        }
        LogUtil.e("fin", System.currentTimeMillis() + "---");
        ByteBuffer buffer = ByteBuffer.wrap(buf);
        bmpFingerprint.copyPixelsFromBuffer(buffer);
        return  bmpFingerprint;

    }
    private void cancelCall(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data",msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }
    private void readFingerVer(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain){
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message",  msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }

    private void fingerRegist(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain){
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message",  msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }

    private  void  readFingerFeature(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain){
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data", msg.getRespond());
            if (msg.getExtion().toByteArray() != null)
                bundle.putByteArray("image",msg.getExtion().toByteArray());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message",  msg.getRespond());
            LogUtil.e("message",msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);


    }



}
